<template>
  <div>
    <!--1视图-->
    <el-form :inline="true" :model="user" ref="ruleForm" :rules="rules"  class="demo-form-inline">
      <el-form-item label="用户名" prop="uname">
        <el-input v-model="user.uname" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input v-model="user.pwd" placeholder="密码" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      //2属性定义
      user:{},
      rules: {
        uname: [
          {required: true, message: '请选择用户名', trigger: 'change'}
        ],
        pwd: [
          {required: true, message: '请选择密码', trigger: 'change'}
        ],
      }
    }
  }, methods: {
    //3自定义方法
    login(ruleForm){
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.axios.post("auth/login",this.user).then(res=>{
            console.log(res.data.data)
            console.log(res.data.code)
            if(res.data.code==200){
                //存token
              this.$message.success(res.data.msg);
              sessionStorage.setItem("token",res.data.data);
              this.$router.push("/user");
            }else{
              this.$message.success(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    }
  }, created() {
    //4.钩子函数
  }
}
</script>

<style scoped>

</style>